package cc.comac.mom.datahandlers.entity;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.QualityQarBasicStandardRequirement;
import cc.comac.mom.dao.entity.QualityQarBasicStandardRequirementDao;
import cc.comac.mom.entity.QualityQarBasicStandardRequirementEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.ResultBean;
@Component
@Transactional
public class QualityQarBasicStandardRequirementDataHandler extends ServiceImpl<QualityQarBasicStandardRequirementDao, QualityQarBasicStandardRequirementEntity>{

	@Autowired
	private QualityQarBasicStandardRequirementDao qualityQarBasicStandardRequirementDao;
	
	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;
	
	public ResultBean batchInsertQarBasicStandardRequirement(List<QualityQarBasicStandardRequirement> list) {
		ResultBean resultBean = new ResultBean(true);
		if (list.size() > 0) {
			List<QualityQarBasicStandardRequirementEntity> entityList = new ArrayList<QualityQarBasicStandardRequirementEntity>();
			for (QualityQarBasicStandardRequirement qualityQarBasicStandardRequirement : list) {
				QualityQarBasicStandardRequirementEntity innerData = qualityQarBasicStandardRequirement.getInnerData();
				if (innerData.getQarBasicStandardId() == 0) {
					throw new RuntimeException("无法获取检验数据！");
				}
				baseEntityDataUtil.setBaseEntityData(innerData);
				entityList.add(innerData);
			}
			saveBatch(entityList, entityList.size());
		}
		return resultBean;
	}
	// 查询实验数据和特征信息信息
	public List<QualityQarBasicStandardRequirement> selectByQarBasicStandardId(long qarBasicStandardId) {
		QueryWrapper<QualityQarBasicStandardRequirementEntity> queryWrapper = new QueryWrapper<QualityQarBasicStandardRequirementEntity>();
		queryWrapper.eq("row_status", 1);
		queryWrapper.eq("qar_basic_standard_id", qarBasicStandardId);
		List<QualityQarBasicStandardRequirementEntity> selectList = qualityQarBasicStandardRequirementDao.selectList(queryWrapper);
		ArrayList<QualityQarBasicStandardRequirement> beanList = new ArrayList<QualityQarBasicStandardRequirement>();
		for (QualityQarBasicStandardRequirementEntity qualityQarBasicStandardRequirementEntity : selectList) {
			QualityQarBasicStandardRequirement qualityQarBasicStandardRequirement = new QualityQarBasicStandardRequirement();
			qualityQarBasicStandardRequirement.setInnerData(qualityQarBasicStandardRequirementEntity);
			beanList.add(qualityQarBasicStandardRequirement);
		}
		return beanList;
	}
	
	// 查询通过实验类型
		public List<QualityQarBasicStandardRequirement> selectByQarBasicStandardIdAndType(long qarBasicStandardId,String requirementType) {
			QueryWrapper<QualityQarBasicStandardRequirementEntity> queryWrapper = new QueryWrapper<QualityQarBasicStandardRequirementEntity>();
			queryWrapper.eq("row_status", 1);
			queryWrapper.eq("qar_basic_standard_id", qarBasicStandardId);
			queryWrapper.eq("requirement_type", requirementType);
			List<QualityQarBasicStandardRequirementEntity> selectList = qualityQarBasicStandardRequirementDao.selectList(queryWrapper);
			ArrayList<QualityQarBasicStandardRequirement> beanList = new ArrayList<QualityQarBasicStandardRequirement>();
			for (QualityQarBasicStandardRequirementEntity qualityQarBasicStandardRequirementEntity : selectList) {
				QualityQarBasicStandardRequirement qualityQarBasicStandardRequirement = new QualityQarBasicStandardRequirement();
				qualityQarBasicStandardRequirement.setInnerData(qualityQarBasicStandardRequirementEntity);
				beanList.add(qualityQarBasicStandardRequirement);
			}
			return beanList;
		}

	// 通过母本主键集合删除对应数据
	public ResultBean batchDeleteByQarBasicStandardIds(List<Long> ids) {
		ResultBean resultBean = new ResultBean(true);
		if (ids.size() > 0) {
			// 通过母本主键查询对应的数据
			QueryWrapper<QualityQarBasicStandardRequirementEntity> queryWrapper = new QueryWrapper<QualityQarBasicStandardRequirementEntity>();
			queryWrapper.eq("row_status", 1);
			queryWrapper.in("qar_basic_standard_id",ids);
			List<QualityQarBasicStandardRequirementEntity> selectList = qualityQarBasicStandardRequirementDao.selectList(queryWrapper);
			if (selectList.size() > 0) {
				for (QualityQarBasicStandardRequirementEntity qualityQarBasicStandardRequirementEntity : selectList) {
					qualityQarBasicStandardRequirementEntity.setRowStatus(0);
					baseEntityDataUtil.updateBaseEntityData(qualityQarBasicStandardRequirementEntity);
				}
				updateBatchById(selectList, selectList.size());
			}
		}
		return resultBean;
	}
	// 物理删除对应数据通过母本主键
	public ResultBean removeByQarBasicStandardId(long qarBasicStandardId) {
		ResultBean resultBean = new ResultBean(true);
		QueryWrapper<QualityQarBasicStandardRequirementEntity> queryWrapper = new QueryWrapper<QualityQarBasicStandardRequirementEntity>();
		queryWrapper.eq("qar_basic_standard_id", qarBasicStandardId);
		qualityQarBasicStandardRequirementDao.delete(queryWrapper);
		return resultBean;
	}
	
	// 物理删除对应数据通过母本主键集合
	public ResultBean removeByQarBasicStandardId(List<Long> qarBasicStandardIds) {
		ResultBean resultBean = new ResultBean(true);
		QueryWrapper<QualityQarBasicStandardRequirementEntity> queryWrapper = new QueryWrapper<QualityQarBasicStandardRequirementEntity>();
		queryWrapper.in("qar_basic_standard_id", qarBasicStandardIds);
		qualityQarBasicStandardRequirementDao.delete(queryWrapper);
		return resultBean;
	}
	
	// 新增或者删除对应数据
	public ResultBean addOrUpdateDatas (List<QualityQarBasicStandardRequirement> list) {
		ResultBean resultBean = new ResultBean(true);
		List<QualityQarBasicStandardRequirementEntity> entityList = new ArrayList<QualityQarBasicStandardRequirementEntity>();
		for (QualityQarBasicStandardRequirement qualityQarBasicStandardRequirement : list) {
			QualityQarBasicStandardRequirementEntity innerData = qualityQarBasicStandardRequirement.getInnerData();
			if (innerData.getId() == 0) {
				baseEntityDataUtil.setBaseEntityData(innerData);
			}else {
				baseEntityDataUtil.updateBaseEntityData(innerData);
			}
			entityList.add(innerData);
		}
		saveOrUpdateBatch(entityList, entityList.size());
		return resultBean;
	}
}
